package com.Allen.Cotroller;

import com.Allen.pojo.User;
import com.Allen.service.UserService;
import com.Allen.utils.JwtHelper;
import com.Allen.utils.Result;
import com.Allen.utils.ResultCodeEnum;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 登录需求
     * 地址: /user/login
     * 方式: post
     */
    @PostMapping("login")
    public Result login(@RequestBody User user){
        Result result = userService.login(user);
        System.out.println("result = " + result);
        return result;
    }

    /**
     * 通过token获取用户数据
     * 地址:/user/getUserInfo
     * 方式: get
     * @param token
     * @return
     */
    @GetMapping("getUserInfo")
    public Result userInfo(@RequestHeader String token){
        Result result =  userService.getUserInfo(token);
        return result;
    }

    /**
     * 注册用户名检测
     * 地址:/user/checkUserName
     * 方式:post
     * @param username
     * @return
     */
    @PostMapping("checkUserName")
    public Result checkUserName(String username){
        Result result = userService.checkUserName(username);
        return result;
    }

    /**
     * 用户注册接口
     * 地址：/user/regist
     * 方式：post
     * @param user
     * @return
     */
    @PostMapping("regist")
    public Result regist(@RequestBody User user){
        Result result = userService.regist(user);
        return result;
    }

    /**
     * 登录检查
     * @param token
     * @return
     */
    @GetMapping("checkLogin")
    public Result checkLogin(@RequestHeader String token){
        if(StringUtils.isEmpty(token) || jwtHelper.isExpiration(token)){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        return Result.ok(null);
    }


}